package com.zhouke.certificate.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.zhouke.certificate.common.Result;
import com.zhouke.certificate.entity.CountMonth;
import com.zhouke.certificate.entity.User;
import com.zhouke.certificate.mapper.CertificatesMapper;
import com.zhouke.certificate.mapper.UserMapper;
import com.zhouke.certificate.service.ICertificatesService;
import com.zhouke.certificate.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Resource
    private IUserService userService;
    @Resource
    private ICertificatesService certificatesService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CertificatesMapper certificatesMapper;

    @GetMapping("/user/count")//获取用户总数
    public Result getUserCount(){
        return Result.success(userService.count());
    }
    @GetMapping("/certificate/count")//获取证书总数
    public Result getCertificateCount(){
        return Result.success(certificatesService.count());
    }
    @GetMapping("/user/yAxisData")//获取用户按月统计的数据
    public Result getUserYAxisData(){
        //获取当前年份
        String year=new SimpleDateFormat("yyyy").format(new Date());
        List<CountMonth> monthList=userMapper.countUserByMonth(year);//获取当年每月的用户数量
        Integer[] ls=new Integer[12];
        for(int i=0;i<ls.length;i++){
            ls[i]=0;
        }
        for (CountMonth countMonth:monthList){
            ls[countMonth.getMonth()-1]=countMonth.getCount();//设置每月对应的用户数
        }
        List<Integer> list=new ArrayList<>(Arrays.asList(ls));
        return Result.success(list);
    }
    @GetMapping("/certificate/yAxisData") //获取证书按月统计的数据
    public Result getCertificateYAxisData(){
        //获取当前年份
        String year=new SimpleDateFormat("yyyy").format(new Date());
        List<CountMonth> monthList=certificatesMapper.countUserByMonth(year);//获取当年每月的用户数量
        Integer[] ls=new Integer[12];
        for(int i=0;i<ls.length;i++){
            ls[i]=0;
        }
        for (CountMonth countMonth:monthList){
            ls[countMonth.getMonth()-1]=countMonth.getCount();//设置每月对应的用户数
        }
        List<Integer> list=new ArrayList<>(Arrays.asList(ls));
        return Result.success(list);
    }

    @GetMapping("/example")
    public Result get(){
        Map<String,Object> map=new HashMap<>();
        map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
        map.put("y", CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
        return Result.success(map);
    }

    @GetMapping("/members")
    public Result members(){
        List<User> list=userService.list();
        int q1=0; //第一季度
        int q2=0;
        int q3=0;
        int q4=0;
        for(User user:list){
            Date createTime = user.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter){
                case Q1:q1+=1;break;
                case Q2:q2+=1;break;
                case Q3:q3+=1;break;
                case Q4:q4+=1;break;
                default:break;
            }
        }
        return Result.success(CollUtil.newArrayList(q1,q2,q3,q4));
    }
}
